﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Objects</title>
    <a href="http://javascriptissexy.com/javascript-objects-in-detail/">Objects in JS</a>
</head>
<body>
    <p>Has five simple data types: Number, String, Boolean, Undefined, and Null. Note that these simple (primitive) data types are immutable</p>
    <p>Property names can be a string or a number, but if the property name is a number, it has to be accessed with the bracket notation</p>
    <div>
        Object Data Properties Have Attributes<br />
        Each data property (object property that store data) has not only the name-value pair, but also 3 attributes (the three attributes are set to true by default):<br />
        <ul>
            <li>Configurable Attribute: Specifies whether the property can be deleted or changed.</li>
            <li>Enumerable: Specifies whether the property can be returned in a for/in loop.</li>
            <li>Writable: Specifies whether the property can be changed.</li>
        </ul>
    </div>

    <div>
        <strong>Deleting Properties of an Object</strong><br />
        To delete a property from an object, you use the delete operator. You cannot delete properties that were inherited, nor can you delete properties with their attributes set to configurable. You must delete the inherited properties on the prototype object (where the properties were defined). Also, you cannot delete properties of the global object, which were declared withe var keyword.
    </div>
    <div>
        <p>
            <strong>Serialize and Deserialize Objects</strong><br />
            To transfer your objects via HTTP or to otherwise convert it to a string, you will need to serialize it (convert it to a string); you can use the JSON.stringify
        </p>
        <p>
            To Deserialize your object (convert it to an object from a string), you use the JSON.parse
        </p>
    </div>
</body>
</html>
<script type="text/javascript">
    //creating objects
    //literal
    var myBooks = {};

    //Object Constructor
    var mango = new Object();

    //Constructor Pattern for Creating Objects
    function Fruit(theColor) {
        this.color = theColor;
    }
    var mangoFruit = new Fruit("Yellow");

    //Prototype Pattern for Creating Objects
    function Fruit() { }
    Fruit.prototype.color = "Yellow";

    // Create a new school object with a property name schoolName
    var school = { schoolName: "MIT" };
    // Prints true because schoolName is an own property on the school object
    console.log("schoolName" in school);  // true
    // Prints true because schoolName is an own property on the school object
    console.log(school.hasOwnProperty("schoolName"));  // true
    // Prints false because the school object inherited the toString method from Object.prototype, therefore toString is not an own property of the school object.
    console.log(school.hasOwnProperty("toString"));  // false 

    //Use of the for/in loop to access the properties in the school object
    for (var eachItem in school) {
        console.log(eachItem); // Prints schoolName, schoolAccredited, schoolLocation
    }

    //delete properties
    var christmasList = { mike: "Book", jason: "sweater" }
    delete christmasList.mike; // deletes the mike property
</script>
